package com.ruoyi.web.core.config;

import com.ruoyi.accountant.service.CurrencyService;
import com.ruoyi.accountant.utils.OSinfo;
import com.ruoyi.system.domain.TCurrency;
import com.ruoyi.system.service.ITCurrencyService;
import com.ruoyi.web.controller.common.CommonController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/***
 * 应用配置
 */
@Configuration
public class AppConf implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private ITCurrencyService currencyService;

    /***
     * 表示关闭的状态 未删除
     */
    public static final int OFF = 0;
    /***
     * 表示开启的状态 已删除
     */
    public static final int ON = 1;

    /***
     * 表示根
     */
    public static final long ROOT = -1; //

    /***
     * 设置ID
     */
    public static final Long APP_SETTING_ID = 1L;

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    /***
     * 登录的用户key
     */
    public static final String SESSION_USER_KEY = "login_user";

    /***
     * 默认保存90天删除的交易记录
     */
    public static final int SAVE_DELETED_TRANSACTION_DETAIL_DAY = 90;

    public static final String PAYTYPE_INCOME="收入";
    public static final String PAYTYPE_PAY="支出";
    public static final String PAYTYPE_TRANSFER="转移";

    @Resource
    private CommonController commonController;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("請到");
        WebApplicationContext applicationContext = (WebApplicationContext) contextRefreshedEvent.getApplicationContext();
        ServletContext sc = applicationContext.getServletContext();
        List<TCurrency> currencyList = currencyService.selectTCurrencyList(null);
        sc.setAttribute("currencyList", currencyList);


    }

    /***
     * 文件上传路径
     * @return
     */
    public static String getUploadPath() {
        if(OSinfo.isLinux()) {
            return "/data/upload/";
        } else {
            return "d:\\upload\\";
        }
    }
}
